package com.neu;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：geekvan
 * @date ：2020/11/7 10:54
 */
public class ExcuteLinux {

    public static String exeCmd(String commandStr) {

        String result = null;
        commandStr=commandStr.trim();
        int tag=0;
        String[]ref=commandStr.split("\\s+");
        if(ref[0].compareToIgnoreCase("copy")==0){
            tag=1;
        }
        try {
            commandStr="\""+commandStr+"\"";
            String[] cmd = new String[]{"/bin/bash", "-c","echo "+commandStr+ "| ./bin/omnisql neu -u admin -p HyperInteractive --port 6274"};//excute neugsql server
            Process ps = Runtime.getRuntime().exec(cmd);

            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
            String line;
            ArrayList<String> list=new ArrayList<>();
            StringBuffer sb=new StringBuffer();
            while ((line = br.readLine()) != null) {
                //执行结果加上回车
                list.add(line);
            }
            if(list.size()<=2){
                result="end!";
            }
            else{
                list.remove(0);
                list.remove(list.size()-1-tag);
                for(String s:list){
                    sb.append(s).append("\n");
                }
                result=sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;

    }
}
